#! /bin/sh
# PCP QA Test No. 1111
# checks basic pcp2influxdb functionality
#
# Copyright (c) 2015-2018 Red Hat.
#
seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
which socat >/dev/null 2>&1 || _notrun "socat binary not installed"
which pcp2influxdb >$seq_full 2>&1 || _notrun "pcp2influxdb not installed"

$python -c "import requests" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python requests module not installed"
major=`$python -c 'import requests; print(requests.__version__.split(".")[0])'`
[ $major -ge 1 ] || _notrun "python requests module too old (v$major)"

port=8086
$PCP_BINADM_DIR/telnet-probe -c localhost $port \
	&& _notrun "Someone already listening on typical InfluxDB $port"

signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!

trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

_full_stash()
{
    echo "--- pcp2influxdb stdout --" >>$seq_full
    cat $tmp.p2i.out >>$seq_full
    echo "--- pcp2influxdb stderr --" >>$seq_full
    cat $tmp.p2i.err >>$seq_full
    echo "--- socket cat stdout --" >>$seq_full
    cat $tmp.socat.out >>$seq_full
    echo "--- socket cat stderr --" >>$seq_full
    cat $tmp.socat.err >>$seq_full
}

pcp2influxdb="$python `which pcp2influxdb`"

echo | tee -a $seq_full
echo "=== 1. pcp2influxdb one-shot noauth ===" | tee -a $seq_full
socat tcp-listen:$port,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb -x roar -t 1 hinv.ncpu sample.control >$tmp.p2i.out 2>$tmp.p2i.err &
p2ipid=$!
sleep 3
$signal $pid $p2ipid >>$seq_full 2>&1
wait
grep -E -q "^POST /write\?db=roar" $tmp.socat.out
[ $? -eq 0 ] && echo "Found POST URL in noauth output"
grep -E -q "hinv_ncpu value=[0-9]+(\.[0-9]+)? [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found proper metric body in noauth output"
grep -E -q "sample_control value= [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found correct null value in noauth output"
_full_stash

echo "=== 2. pcp2influxdb noauth with tags ===" | tee -a $seq_full
socat tcp-listen:$port,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb -x roar -X host=me.example.com -t 1 hinv.ncpu \
        >$tmp.p2i.out 2>$tmp.p2i.err &
p2ipid=$!
sleep 3
$signal $pid $p2ipid >>$seq_full 2>&1
wait
grep -E -q "^POST /write\?db=roar" $tmp.socat.out
[ $? -eq 0 ] && echo "Found POST URL in noauth output"
grep -E -q "hinv_ncpu,host=me\.example\.com value=[0-9]+(\.[0-9]+)? [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found proper metric body in noauth output"
_full_stash

echo "=== 3. pcp2influxdb auth with tags ===" | tee -a $seq_full
socat tcp-listen:$port,reuseaddr - >$tmp.socat.out 2>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb -x roar --db-user pcp --db-pass hey \
        --db-tags host=me.example.com -t 1 hinv.ncpu \
        >$tmp.p2i.out 2>$tmp.p2i.err &
p2ipid=$!
sleep 3
$signal $pid $p2ipid >>$seq_full 2>&1
wait
grep -E -q "^POST /write\?db=roar" $tmp.socat.out
[ $? -eq 0 ] && echo "Found POST URL in output"
grep -E -q "Authorization: Basic cGNwOmhleQ==" $tmp.socat.out
[ $? -eq 0 ] && echo "Found auth data in HTTP request"
grep -E -q "hinv_ncpu,host=me\.example\.com value=[0-9]+(\.[0-9]+)? [0-9]+$" $tmp.socat.out
[ $? -eq 0 ] && echo "Found proper metric body in output"
_full_stash

echo "=== 4. pcp2influxdb bad-instance tolerance ===" | tee -a $seq_full
rm -f $tmp.socat.out $tmp.socat.err
socat tcp-listen:$port,reuseaddr - >>$tmp.socat.out 2>>$tmp.socat.err &
pid=$!
sleep 2
$pcp2influxdb -t 2 -I sample.aggregate.null sample.string.hullo sample.bad >$tmp.p2i.out 2>$tmp.p2i.err &
p2ipid=$!
sleep 8 # enough for at least 2 messages
$signal $pid $p2ipid >>$seq_full 2>&1
wait
grep 'Excluding' $tmp.p2i.err
_full_stash

status=0
exit
